package cn.edu.cdp.dao;

import cn.edu.cdp.domain.Course;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface CourseDao {
    @Select("select cu.course_id courseId, cu.name, cu.credit, cu.semester, cl.name collegeName " +
            "from course cu left join college cl on cu.college_id=cl.college_id")
    List<Map<String, Object>> allCourse();

    @Select("select course_id courseId, name, credit, semester, college_id collegeId " +
            "from course where course_id=#{courseId}")
    Course getCourseById(String courseId);

    @Select("select stu.student_id studentId, stu.name, s.score from study s, student stu " +
            "where s.student_id=stu.student_id and s.course_id=#{courseId}")
    List<Map<String, Object>> scoreList(String courseId);

    @Insert("insert into course (course_id, name, credit, semester, college_id) " +
            "values (#{courseId}, #{name}, #{credit}, #{semester}, #{collegeId})")
    void insert(Course course);

    @Update("update course set name=#{name}, credit=#{credit}, semester=#{semester}, " +
            "college_id=#{collegeId} where course_id=#{courseId}")
    void update(Course course);

    @Delete("delete from course where course_id=#{courseId}")
    void deleteById(String courseId);
}
